package cn.iocoder.boot.MLlib;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.streaming.StreamingContext;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SparkConfig {

    @Bean
    public SparkSession sparkSession() {
        SparkConf sparkConf = new SparkConf()
                .setMaster("local[*]")
                .setAppName("SparkStreamingMLlibApp");
        return SparkSession.builder()
                .config(sparkConf)
                .getOrCreate();
    }

    @Bean
    public JavaSparkContext javaSparkContext(SparkSession sparkSession) {
        return new JavaSparkContext(sparkSession.sparkContext());
    }

    @Bean
    public JavaStreamingContext javaStreamingContext(JavaSparkContext javaSparkContext) {
        return new JavaStreamingContext(javaSparkContext, org.apache.spark.streaming.Duration.apply(1000));
    }
}